package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsEvaluationAgency;
import com.ruoyi.system.domain.news.dto.NewsEvaluationAgencyDto;
import com.ruoyi.system.domain.news.query.NewsEvaluationAgencyQuery;
import com.ruoyi.system.domain.news.vo.NewsEvaluationAgencyVo;
import com.ruoyi.system.service.news.NewsEvaluationAgencyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsEvaluationAgency")
@Api(value = "newsEvaluationAgencyController", tags  = "测评机构")
public class NewsEvaluationAgencyController extends BaseController {

	@Resource
	private NewsEvaluationAgencyService newsEvaluationAgencyService;

    @ApiOperation(value = "新增/修改测评机构信息", notes = "新增/修改测评机构信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsEvaluationAgencyDto newsEvaluationAgencyDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsEvaluationAgency newsEvaluationAgency = new NewsEvaluationAgency();
		BeanUtils.copyProperties(newsEvaluationAgencyDto,newsEvaluationAgency);
		if (newsEvaluationAgency.getId()==null) {
			newsEvaluationAgency.setCreateBy(loginUser.getUsername());
			newsEvaluationAgency.setDeptId(loginUser.getDeptId());
			newsEvaluationAgency.setCreateTime(new Date());
			newsEvaluationAgency.setUpdateBy(loginUser.getUsername());
			newsEvaluationAgency.setUpdateTime(new Date());
		} else {
			newsEvaluationAgency.setUpdateBy(loginUser.getUsername());
			newsEvaluationAgency.setUpdateTime(new Date());
		}
		boolean result = newsEvaluationAgencyService.saveOrUpdate(newsEvaluationAgency);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除测评机构信息", notes = "删除测评机构信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsEvaluationAgencyService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个测评机构信息", notes = "获取单个测评机构信息")
	@GetMapping("/getById")
	public R<NewsEvaluationAgencyVo> getById(@RequestParam(value = "id") Long id) {
		NewsEvaluationAgencyQuery newsEvaluationAgencyQuery = new NewsEvaluationAgencyQuery();
		newsEvaluationAgencyQuery.setId(id);
		List<NewsEvaluationAgencyVo> newsEvaluationAgencyList = newsEvaluationAgencyService.getNewsEvaluationAgencyList(newsEvaluationAgencyQuery);
		if (newsEvaluationAgencyList!=null&&newsEvaluationAgencyList.size()>0) {
			return R.ok(newsEvaluationAgencyList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取测评机构信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsEvaluationAgencyVo>> list(@RequestBody NewsEvaluationAgencyQuery newsEvaluationAgencyQuery) {
		List<NewsEvaluationAgencyVo> newsEvaluationAgencyList = newsEvaluationAgencyService.getNewsEvaluationAgencyList(newsEvaluationAgencyQuery);
		return R.ok(new PageInfo<>(newsEvaluationAgencyList));
	}
}
